<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/06/20
 * Time: 上午10:53
 */
class MaintenanceStationDetails extends Eloquent
{
    protected $table = "maintenance_station_details";

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-20 11:07:29
     * @Description: 工位占用详细信息入库(开始时间)门店
     * @Param: $shop_id 门店id
     * @Param: $station_id 工位id
     * @Param: $car_no 车牌号
     * @Return: Object
     */
    public static function InsertDetails($station_id, $shop_id){
        $mscs=MaintenanceStationCarStatus::where("shop_id",$shop_id)->whereIn("status",array(1,2))->where("station_id",$station_id)->first();
        $station_name = MaintenanceStation::where("shop_id",$shop_id)->where("id",$station_id)->first();
        $name = !is_null($station_name) ? $station_name->name : '';
        $type = !is_null($station_name) ? $station_name->type : '';
        if(is_null($mscs)){
            $details = '0';   //工位占用信息不存在
        }else {

            //处理先入位，后生成订单
            $car_no = MaintenanceStationDetails::where("shop_id", $shop_id)->where("station_id", $station_id)->where("occupy_end_time", "0000-00-00 00:00:00")->where("order_id","0")->where("car_no",$mscs->car_no)->first();
            if(!is_null($car_no)){
                $now_time = date("Y-m-d H:i:s", time());
                $result1 = MaintenanceStationDetails::where("id", $car_no->id)
                    ->update(array("order_id" => $mscs->order_id, "payable_amount" => "0", "occupy_end_time" => $now_time, "updated_at" => date("Y-m-d H:i:s", time())));
                if ($result1) {
                    //操作成功记录到统计表中
                    MaintenanceStationLengthTime::SelectOne($shop_id, $station_id, $name, $type, $car_no->occupy_start_time, $now_time);
                }
            }

            $arr = MaintenanceStationDetails::where("shop_id", $shop_id)->where("station_id", $station_id)->where("occupy_end_time", "0000-00-00 00:00:00")->first();
            if (!is_null($arr)) {
                //$details = '3'; //车辆已入位
                //复检时候如果查询有结束时间为0000-00-00 00:00:00先修改结束时间，再另开新的计时
                $now_time = date("Y-m-d H:i:s", time());
                $result = MaintenanceStationDetails::where("id", $arr->id)
                    ->update(array("payable_amount" => "0", "occupy_end_time" => $now_time, "updated_at" => date("Y-m-d H:i:s", time())));
                if ($result) {
                    //操作成功记录到统计表中
                    MaintenanceStationLengthTime::SelectOne($shop_id, $station_id, $name, $type, $arr->occupy_start_time, $now_time);
                }
            }


            $station_details = new MaintenanceStationDetails();
            if (!is_null($mscs->order_id)) {
                $order = Orders::where("order_id", $mscs->order_id)->first();
                if (is_null($order)) {
                    $details = '1';  //工位关联订单不存在
                } else {
                    if(!$order->payable_amount || $order->payable_amount == "" || $order->payable_amount == null){
                        $order->payable_amount = '0';
                    }
                    $station_details->payable_amount = $order->payable_amount;
                }
            } else {
                $station_details->payable_amount = '0';
            }
            $station_details->station_name = $name;
            $station_details->order_id = $mscs->order_id;
            $station_details->shop_id = $mscs->shop_id;
            $station_details->station_id = $mscs->station_id;
            $station_details->car_no = $mscs->car_no;
            $station_details->occupy_start_time = date("Y-m-d H:i:s", time());
            $station_details->year_month = date("Y-m", time());
            $station_details->created_at = date("Y-m-d H:i:s", time());
            $station_details->updated_at = date("Y-m-d H:i:s", time());
            if ($station_details->save()) {
                $details = '2';  //工位统计保存成功
            } else {
                $details = '4';  //工位统计保存失败
            }
            return $details;
        }
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-06-20 17:55:29
     * @Description: 工位占用详细信息入库(结束时间)门店
     * @Param: $shop_id 门店id
     * @Param: $station_id 工位id
     * @Param: $car_no 车牌号
     * @Return: Object
     */
    public static function UpdatedDetails($order_id, $shop_id){
        //判断车辆是否入位
        $mscs=MaintenanceStationCarStatus::where("shop_id",$shop_id)->whereIn("status",array(1,2))->where("order_id",$order_id)->first();
        $arr = \DB::getQueryLog();
        if(is_null($mscs)){
            $details = '0';   //工位占用信息不存在
        }else{
            $station_name = MaintenanceStation::where("id",$mscs->station_id)->first();
            $name = !is_null($station_name) ? $station_name->name : '';
            $type = !is_null($station_name) ? $station_name->type : '';
            //判断车辆入位后是否出位
            $station_details = MaintenanceStationDetails::where("order_id",$order_id)->where("shop_id",$shop_id)->where("station_id",$mscs->station_id)->where("occupy_end_time", "0000-00-00 00:00:00")->first();
            if(!is_null($station_details)){
                //车辆未出位，修改信息
                if(!is_null($mscs->order_id)){
                    $order = Orders::where("order_id",$order_id)->first();
                    if(is_null($order)){
                        $details = '1';  //工位关联订单不存在
                    }else{
                        if(!$order->payable_amount || $order->payable_amount == "" || $order->payable_amount == null){
                            $order->payable_amount = '0';
                        }
                        $payable_amount = $order->payable_amount;
                    }
                }else{
                    $payable_amount = '0';
                }
                $arr = MaintenanceStationDetails::where("shop_id", $shop_id)->where("station_id", $mscs->station_id)->where("occupy_end_time", "0000-00-00 00:00:00")->first();
                $now_time = date("Y-m-d H:i:s",time());
                //$result=MaintenanceStationDetails::where('order_id', $order_id)
                  //  ->where("shop_id",$shop_id)->where("station_id",$mscs->station_id)
                  //  ->update(array("payable_amount"=>$payable_amount, "occupy_end_time"=>$now_time, "updated_at"=>date("Y-m-d H:i:s",time())));
                $result=MaintenanceStationDetails::where('id', $arr->id)->update(array("payable_amount"=>$payable_amount, "occupy_end_time"=>$now_time, "updated_at"=>date("Y-m-d H:i:s",time())));
                if($result){
                    //操作成功记录到统计表中
                    MaintenanceStationLengthTime::SelectOne($shop_id, $mscs->station_id, $name, $type,$station_details->occupy_start_time, $now_time);
                    $details = '2';  //工位统计保存成功
                }else{
                    $details = '4';  //工位统计保存失败
                }
            }else{
                $details = '5';   //车辆入位后出位不执行任何操作
            }
        }
        return $details;

    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-06-21 15:09:29
     * @Description: 订单结算成功记录工位赚取的金额
     * @Param: $shop_id 门店id
     * @Param: $station_id 工位id
     * @Param: $car_no 车牌号
     * @Return: Object
     */
    public static function payable_amount($order_id, $shop_id, $payable_amount){
        $details = MaintenanceStationDetails::where("order_id",$order_id)->update(array("payable_amount"=>$payable_amount, "updated_at"=>date("Y-m-d H:i:s",time())));
        //查询车辆第一次进入工位的工位ID
        $arr = MaintenanceStationDetails::where("order_id",$order_id)->orderBy("created_at","asc")->first();
        $zong_time = MaintenanceStationDetails::where("order_id",$order_id)->orderBy("created_at","asc")->get();

        //

        if(!is_null($arr)){
            $time = 0;
            if(!is_null($zong_time)){
                foreach($zong_time as $key => $val){
                    //$time += $val->occupy_end_time - $val->occupy_start_time;
                    $times = MaintenanceStationLengthTime::str_time($val->occupy_start_time, $val->occupy_end_time, $shop_id);
                    $time += $times;
                }
                $time = MaintenanceStationLengthTime::format_date($time);

                $time = MaintenanceStationLengthTime::length_time('00:00:00',$time);
            }
            $month = date("Y-m",time());
            $length_time = MaintenanceStationLengthTime::where("shop_id",$shop_id)
                ->where("station_id",$arr['station_id'])
                ->where('created_at','like','%'.$month.'%')
                ->first();
            if(!empty($length_time)){
                MaintenanceStationLengthTime::where("station_id",$arr['station_id'])
                    ->where("shop_id",$shop_id)
                    ->where('created_at','like','%'.$month.'%')
                    ->update(array("pay_price"=>$length_time->payable_amount+$payable_amount, "length_time"=>$time, "count_car"=>$length_time->count_car+1, "updated_at"=>date("Y-m-d H:i:s",time())));
            }else{
                MaintenanceStationLengthTime::where("station_id",$arr['station_id'])
                    ->where("shop_id",$shop_id)
                    ->where('created_at','like','%'.$month.'%')
                    ->update(array("pay_price"=>$payable_amount, "length_time"=>$time, "count_car"=>1, "updated_at"=>date("Y-m-d H:i:s",time())));
            }

        }
    }
}